package com.x.erp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.x.erp.dao.CarDao;
import com.x.erp.dao.UserDao;
import com.x.erp.model.Car;
import com.x.erp.model.User;
import com.x.erp.rpc.ChuanCheRpcService;
import com.x.framework.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 传车服务
 */
@Component
@Service(interfaceName = "com.x.erp.rpc.ChuanCheRpcService", timeout = 60000)
public class ChuanCheService extends BaseService implements ChuanCheRpcService {

	private Logger logger = LoggerFactory.getLogger(SouHuCarService.class);

	@Autowired
	private FirstCarService firstCarService;

	@Autowired
	private SouHuCarService souHuCarService;

	@Autowired
	private ZhiTongService zhiTongService;

	@Autowired
	private CarDao carDao;

	@Autowired
	private UserDao userDao;

	@Override
	public void chuanChe(Car car) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String sql = "SELECT " +
						"DISTINCT CAR_ID " +
						"FROM " +
						"erp_car e " +
						"LEFT JOIN erp_model_mapping_thirdparty em ON e.MODEL_ID = em.MODEL_ID " +
						"WHERE " +
						"e.CAR_SALE_STATE = 20 " +
						"AND e.CAR_STOCK_STATE = 20 " +
						"AND em.MODEL_THIRDPARTY_ID != 0 " +
						"AND e.CAR_RETAIL_PRICE != 0 " +
						"AND e.CAR_LICENCE_STATE != 3";
				List<Car> cars = null;
				try {
					cars = carDao.queryForList(sql, Car.class);
					logger.info(cars.size() + "");
				} catch (Exception e) {
					e.printStackTrace();
				}
				for (Car car : cars){
					firstCarService.insertOrUpdateCarFirst(car);
					souHuCarService.updateCarSaleState20(car);
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	@Override
	public void chuanShop(User user) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String sql = "SELECT " +
						"eu.USER_ID " +
						"FROM " +
						"erp_user eu " +
						"WHERE " +
						"eu.USER_TYPE = 40 ";
				List<User> users = null;
				try {
					users = userDao.queryForList(sql, User.class);
					logger.info(users.size() + "");
				} catch (Exception e) {
					e.printStackTrace();
				}
				for (User user1 : users){
					zhiTongService.insertShop(user1);
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}


}
